package top.gangster.lunan.userservice.service;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import java.util.Date;
import org.springframework.stereotype.Service;
import top.gangster.lunan.userservice.entity.User;

/**
 * <p>
 * token服务
 * </p>
 *
 * @author vic
 * @since 2021-11-06
 */
@Service
public class TokenService {

  /**
   * 过期时间一天
   */
  private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000;

  public String getToken(User user) {
    Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
    String token = "";
    token = JWT.create().withAudience(user.getUserId(), user.getUsername(), user.getPassword(),user.getRoleId(),user.getUid())
        .withExpiresAt(date) //60*24分钟后token过期
        .sign(Algorithm.HMAC256(user.getPassword())); // 以 password 作为 token 的密钥
    return token;
  }
}
